class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        numDict = {}
        for i in range(len(nums)):
            if nums[i] in numDict:
                j = numDict[nums[i]]
                if abs(j-i)<=k:
                    return True
                numDict[nums[i]]=i   
            else:
                numDict.update({nums[i]:i})
        return False

print(Solution().containsNearbyDuplicate([1,2,1], 0))